using System;
using System.Collections.Generic;
using System.Linq;
using Aluetjen.EmailService.Domain.Gateways;
using Aluetjen.EmailService.Domain.Infrastructure;
using Aluetjen.EmailService.Domain.Models.Pipeline;
using Aluetjen.EmailService.Domain.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;

namespace Aluetjen.EmailService.Domain.Tests.Services
{
    public class DispatchServiceTestsBase
    {
        private readonly List<Gateway> _gateways = new List<Gateway>();
        private Func<string, IGatewayImpl> _gatewayImplFactory;
        private IRepository<Gateway> _gatewayRepository;

        [TestInitialize]
        public void Initialize()
        {
            _gateways.Clear();
        }

        protected void SetUpRepository()
        {
            _gatewayRepository.Stub(r => r.All()).Return(_gateways.AsQueryable());
        }

        protected void SetUpGateway(string gatewayName, out IGatewayImpl gatewayImpl, out Gateway gateway)
        {
            gatewayImpl = MockRepository.GenerateMock<IGatewayImpl>();
            gateway = new Gateway
                          {
                              Name = gatewayName,
                              DispatchToGateway = true,
                              IgnoreFailure = false
                          };

            AddGatewayImpl(gatewayName, gatewayImpl);
            AddGateway(gateway);
        }

        protected void SetUpMocks()
        {
            _gatewayImplFactory = MockRepository.GenerateStub<Func<string, IGatewayImpl>>();
            _gatewayRepository = MockRepository.GenerateStub<IRepository<Gateway>>();
        }

        protected void AddGatewayImpl(string gatewayName, IGatewayImpl gatewayImpl)
        {
            _gatewayImplFactory.Stub(f => f(gatewayName)).Return(gatewayImpl);
        }

        protected void AddGateway(Gateway gtw)
        {
            _gateways.Add(gtw);
        }

        protected DispatchService GetInstance()
        {
            return new DispatchService(_gatewayRepository, _gatewayImplFactory);
        }
    }
}